package com.example.myapplication.carinspection;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LeavecommentBean;
import com.example.myapplication.ShuJu.ListfvehiclesBean;
import com.example.myapplication.apter.ListfvehiclesApter;

import org.json.JSONException;
import org.json.JSONObject;

import java.security.ProtectionDomain;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class VehiclemanagementnActivity extends AppCompatActivity {

    private ImageView but_lawyerList;
    private LinearLayout li_appointment;
    private String TAG;
    private RecyclerView re_listfvehicles;
    private ArrayList<String> plateNos;
    private ArrayList<String> travelDates;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vehiclemanagementn);
        initview();
        //返回
        but_bloak();
        //添加车辆
        addcat();

    }

    @Override
    protected void onResume() {
        super.onResume();
        //显示车辆列表
        listfvehicles();
    }

    private void listfvehicles() {
        OKHttpTool.doNetRequest("prod-api/api/park/car/consumption",
                OKHttpTool.GET,
                ListfvehiclesBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ListfvehiclesBean listfvehiclesBean = (ListfvehiclesBean)obj;
                                if(listfvehiclesBean.getCode()==200){
                                    re_listfvehicles.setAdapter(new ListfvehiclesApter(listfvehiclesBean.getRows(),VehiclemanagementnActivity.this,plateNos));
                                    re_listfvehicles.setLayoutManager(new LinearLayoutManager(VehiclemanagementnActivity.this));
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void addcat() {
        li_appointment.setOnClickListener(new View.OnClickListener() {
            private String format;
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(VehiclemanagementnActivity.this);
                View inflate = LayoutInflater.from(VehiclemanagementnActivity.this).inflate(R.layout.item_addcat, null);
                EditText Licensenumber = inflate.findViewById(R.id.Licensenumber);
                EditText amount = inflate.findViewById(R.id.amount);
                EditText gasFilling = inflate.findViewById(R.id.gasFilling);
                TextView yutime = inflate.findViewById(R.id.yutime);
                EditText travelDistance = inflate.findViewById(R.id.travelDistance);
                builder.setView(inflate);

                //预约时间
                yutime.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DatePickerDialog datePickerDialog = new DatePickerDialog(VehiclemanagementnActivity.this, new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                String data = year +"-" +month + "-" +dayOfMonth;
                                //Date date = new Date(year, month, dayOfMonth);
                               // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                                format = data;
                                yutime.setText(data);
                            }
                        }, 2023, 11, 11);
                        datePickerDialog.show();
                    }

                });
                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String CatName = Licensenumber.getText().toString();
                        String amountName = amount.getText().toString();
                        String gasFillingName = gasFilling.getText().toString();
                        String travelDistanceName = travelDistance.getText().toString();
                        //发送请求添加车辆'
                        JSONObject jsonObject = new JSONObject();
                        String JSON = null;
                        try {
                            jsonObject.put("plateNo", CatName);
                            jsonObject.put("amount", amountName);
                            jsonObject.put("travelDistance", travelDistanceName);
                            jsonObject.put("travelDate", format);
                            jsonObject.put("gasFilling", gasFillingName);
                            JSON = jsonObject.toString();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        sendaddcat(JSON);
                    }
                });
                builder.create().show();
            }
        });
    }

    private void sendaddcat(String JSON) {
        OKHttpTool.doNetRequest("prod-api/api/park/car/consumption",
                OKHttpTool.POST,
                LeavecommentBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                LeavecommentBean leavecommentBean = (LeavecommentBean) obj;
                                if (leavecommentBean.getCode() == 200) {
                                    Toast.makeText(VehiclemanagementnActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }, JSON);
    }

    private void but_bloak() {
        but_lawyerList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VehiclemanagementnActivity.this.finish();
            }
        });
    }

    private void initview() {
        but_lawyerList = findViewById(R.id.but_lawyerList);
        li_appointment = findViewById(R.id.li_appointment);
        re_listfvehicles = findViewById(R.id.re_listfvehicles);

    /*    //记入车牌号
        plateNos = new ArrayList();
        //记入行驶日期
        travelDates = new ArrayList();*/
    }
}